package rhinestone;

import rhinestone.addon.ContainerDB;
import rhinestone.TUI.*;
import rhinestone.Vlad_GUI.NewJFrame;
import rhinestone.Arvydas_GUI.Login;
import rhinestone.Control.*;
import rhinestone.Model.ProductContainer;
import rhinestone.Model.MachineryContainer;
import rhinestone.Model.CustomerContainer;
import rhinestone.Model.PackContainer;
import rhinestone.Model.StaffContainer;
import rhinestone.Model.OrderContainer;
import rhinestone.Model.SaleContainer;
import rhinestone.Model.LeaseContainer;
import rhinestone.Model.StatisticCalculator;

/**
 * The launcher of the program.
 *
 * @author Group 2
 * @version 2010.12.12
 */

public class Main {
	// These are file names used to keep serialized containers.
	// They are created in the main project folder, unless you change it to a different path name.
	private final static String PRODUCT_FILE = "product_container.ser";
	private final static String MACHINERY_FILE = "machinery_container.ser";
	private final static String CUSTOMER_FILE = "customer_container.ser";
	private final static String PACK_FILE = "pack_container.ser";
	private final static String STAFF_FILE = "staff_container.ser";
	private final static String ORDER_FILE = "order_container.ser";
	private final static String SALE_FILE = "sale_container.ser";
	private final static String LEASE_FILE = "lease_container.ser";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
		/**
		 * Container initialization.
		 * First it tries to load from from file.
		 * If it returns null, then creates a new container.
		 */
		ProductContainer productContainer = (ProductContainer) ContainerDB.loadContainer(PRODUCT_FILE);
		if(productContainer == null)
			productContainer = ProductContainer.getInstance();

		MachineryContainer machineryContainer = (MachineryContainer) ContainerDB.loadContainer(MACHINERY_FILE);
		if(machineryContainer == null)
			machineryContainer = MachineryContainer.getInstance();

		CustomerContainer customerContainer = (CustomerContainer) ContainerDB.loadContainer(CUSTOMER_FILE);
		if(customerContainer == null)
			customerContainer = CustomerContainer.getInstance();

		PackContainer packContainer = (PackContainer) ContainerDB.loadContainer(PACK_FILE);
		if(packContainer == null)
			packContainer = PackContainer.getInstance();

		StaffContainer staffContainer = (StaffContainer) ContainerDB.loadContainer(STAFF_FILE);
		if(staffContainer == null)
			staffContainer = StaffContainer.getInstance();

		OrderContainer orderContainer = (OrderContainer) ContainerDB.loadContainer(ORDER_FILE);
		if(orderContainer == null)
			orderContainer = OrderContainer.getInstance();

		SaleContainer saleContainer = (SaleContainer) ContainerDB.loadContainer(SALE_FILE);
		if(saleContainer == null)
			saleContainer = SaleContainer.getInstance();

		LeaseContainer leaseContainer = (LeaseContainer) ContainerDB.loadContainer(LEASE_FILE);
		if(leaseContainer == null)
			leaseContainer = LeaseContainer.getInstance();

		StatisticCalculator statisticCalculator = new StatisticCalculator(saleContainer);

		/**
		 * Controller initialization.
		 * The containers are passed in their constructor.
		 */
		ProductController productController = new ProductController(productContainer, machineryContainer, packContainer);
		CustomerController customerController = new CustomerController(customerContainer);
		SaleController saleController = new SaleController(productContainer, customerContainer, packContainer, saleContainer);
		final StaffController staffController = new StaffController(staffContainer);
		OrderController orderController = new OrderController(orderContainer, productContainer);
		LeasingController leasingController = new LeasingController(leaseContainer, machineryContainer);
		StatisticsController statisticsController = new StatisticsController(staffContainer, customerContainer, statisticCalculator);

		/**
		 * UI initialization.
		 * The particular controller is passed to every UI.
		 */
/*		UI saleUI = new SaleUI(saleController);
		UI productUI = new ProductUI(productController);
		UI staffUI = new StaffUI(staffController);
		UI orderUI = new OrderUI(orderController);
		UI customerUI = new CustomerUI(customerController);
		UI leasingUI = new LeaseUI(leasingController, customerController);
		UI statisticsUI = new StatisticsUI(statisticsController);
*/
		/**
		 * Initialize Main Menu UI and launch it starting the program.
		 */
/*		MainMenuUI mainMenu = new MainMenuUI(saleUI, productUI, leasingUI, staffUI, orderUI, customerUI, statisticsUI);
		mainMenu.startMenu();
*/
		java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Login login = new Login(staffController);
				login.setVisible(true);
            }
		});
		NewJFrame gui = new NewJFrame(customerController, leasingController,
			orderController, productController, saleController, staffController, statisticsController);
		gui.setVisible(true);


		/**
		 * After the user finishes using the program by choosing 'Exit' option,
		 * the program checks if there was any changes done in each container.
		 * If was, it serializes them updating the old ones, so the next time
		 * the most recent container is loaded.
		 */
		if(productContainer.isChanged()) {
			System.err.println("Saving product container...");
			productContainer.setChanged(false);
			ContainerDB.saveContainer(productContainer, PRODUCT_FILE);
		}
		if(machineryContainer.isChanged()) {
			System.err.println("Saving machinery container...");
			machineryContainer.setChanged(false);
			ContainerDB.saveContainer(machineryContainer, MACHINERY_FILE);
		}
		if(customerContainer.isChanged()) {
			System.err.println("Saving customer container...");
			customerContainer.setChanged(false);
			ContainerDB.saveContainer(customerContainer, CUSTOMER_FILE);
		}
		if(packContainer.isChanged()) {
			System.err.println("Saving pack container...");
			packContainer.setChanged(false);
			ContainerDB.saveContainer(packContainer, PACK_FILE);
		}
		if(staffContainer.isChanged()) {
			System.err.println("Saving staff container...");
			staffContainer.setChanged(false);
			ContainerDB.saveContainer(staffContainer, STAFF_FILE);
		}
		if(orderContainer.isChanged()) {
			System.err.println("Saving order container...");
			orderContainer.setChanged(false);
			ContainerDB.saveContainer(orderContainer, ORDER_FILE);
		}
		if(saleContainer.isChanged()) {
			System.err.println("Saving sale container...");
			saleContainer.setChanged(false);
			ContainerDB.saveContainer(saleContainer, SALE_FILE);
		}
		if(leaseContainer.isChanged()) {
			System.err.println("Saving lease container...");
			leaseContainer.setChanged(false);
			ContainerDB.saveContainer(leaseContainer, LEASE_FILE);
		}
    }
}
